
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;

public class T {


    public static void simpleTest() {
        int[] array = {22, 45, 2, 5, 8, 4, 2, 1, 5};
//        Sort.insetSort(array);
        insetSort1(array);
        System.out.println(Arrays.toString(array));

    }


    public static void main(String[] args) {
        simpleTest();
    }


    public static void swap(int[] arr, int n1, int n2) {
        int tem = arr[n1];
        arr[n1] = arr[n2];
        arr[n2] = tem;

    }

    public static void insetSort1(int[] arr) {

        for (int i = 1; i < arr.length; i++) {
            int tem = arr[i];
            int j = i - 1;
            for (; j >= 0; j--) {

                if (tem >= arr[j]) {
                    break;
                } else {
                    arr[j + 1] = arr[j];
                }
            }
            arr[j + 1] = tem;

        }
    }

    public static void shellSort(int[] arr) {
        int gap = arr.length;
        while (gap > 1) {

            gap /= 2;
            shell(arr, gap);
        }


    }

    private static void shell(int[] arr, int gap) {
        for (int i = gap; i < arr.length; i++) {
            int tmp = arr[i];
            int j = i - gap;
            for (; j >= 0; j -= gap) {

                if (arr[j] > tmp) {
                    arr[j + gap] = arr[j];
                } else {
                    break;
                }


            }
            arr[j + gap] = tmp;
        }
    }


    public static void selectSort(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            int minIndex = i;
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[j] < arr[minIndex]) {
                    minIndex = j;
                }
            }

            swap(arr, i, minIndex);

        }


    }


    public static void heapSort(int[] arr) {
        creatBig(arr);
        for (int i = arr.length - 1; i > 0; i--) {
            swap(arr, i, 0);
            siftDown(arr, 0, i);
        }
    }

    private static void creatBig(int[] arr) {
        for (int i = (arr.length - 1 - 1)/2; i >= 0; i--) {
            siftDown(arr, i, arr.length);
        }
    }

    private static void siftDown(int[] arr, int parent, int len) {

        int child = 2 * parent + 1;
        while (child < len) {
            if (child + 1 < len && arr[child+1] > arr[child]) {
                child++;

            }

            if (arr[child] > arr[parent]) {
                swap(arr, parent, child);
                parent = child;
                child = 2 * parent + 1;
            } else {
                break;
            }


        }


    }


   public void bubbleSort(int[] arr) {
       for (int i = 0; i < arr.length-1; i++) {
           boolean fla=true;
           for (int j = 0; j < arr.length-1-i; j++) {
               if(arr[j]>arr[j+1]){
                   swap(arr,j+1,j);
                   fla=false;
               }

           }
           if (fla){
               break;
           }
       }



    }

public static void quickSort(int[] arr){
        quick(arr,0,arr.length-1);//装饰一下

}

    private static void quick(int[] arr,int s,int e) {
        if(s>=e){
            return;
        }

        if(e-s+1<=7){    //加一是为了正好数据是7个数据
            insertRange(arr,s,e);
            return;
        }

        int mid=getMid(arr,s,e);  //三数取中法优化快排
        swap(arr,s,mid);

        int pivot=partition(arr,s,e);   //分割两边

        quick(arr,s,pivot-1);   //递归左边
        quick(arr,pivot+1,e);  //递归右边


    }

    private static void insertRange(int[] arr, int s, int e) {
        for (int i = s+1; i <=e ; i++) {
            int tmp=arr[i];
            int j =i-1;
            for ( ; j >=s ; j--) {
                if(arr[j]>arr[i]){
                    arr[j+1]=arr[j];
                }else {
                    break;
                }

            }
            arr[j+1]=arr[i];

        }

    }

    private static int partition(int[] arr, int s, int e) {
        int tmp=arr[s];
        while (s<e){

            while (s<e&&arr[e]>=tmp){
                e--;
            }
            arr[s]=arr[e];

            while (s<e&&arr[s]<=tmp){
                s++;
            }
            arr[e]=arr[s];

        }
        arr[s]=tmp;
        return s;

    }

    private static int getMid(int[] arr, int s, int e) {
        int mid=(e-s)/2;
        if(arr[s]>arr[e]){
            if(arr[mid]>arr[s]){
                return s;
            }

            if(arr[e]>arr[mid]){
                return e;
            }

            return mid;
        }else {
            if(arr[mid]>arr[e]){
                return e;
            }

            if(arr[s]>arr[mid]){
                return s;
            }

            return mid;
        }
    }

public static void mergeSort(int[] arr){


        mergeSortTem( arr,0,arr.length-1);
}

    private static void mergeSortTem(int[] arr, int left, int right) {

        if(left>=right){
            return;
        }

        int mid=(left+right)/2;
        mergeSortTem(arr,left,mid);
        mergeSortTem(arr,mid+1,right);

        merge(arr,left,right,mid);
    }

    private static void merge(int[] arr, int left, int right,int mid) {
        int[] tmp=new int[right-left+1];
        int s1=left;
        int s2=mid+1;
        int k=0;

        while(s1<=mid&&s2<=right){
            if(arr[s1]>arr[s2]){
                tmp[k++]=arr[s2++];
            }else {
                tmp[k++]=arr[s1++];
            }
        }

        while(s1<=mid){
            tmp[k++]=arr[s1++];
        }

        while(s2<right){
            tmp[k++]=arr[s2++];
        }

        for (int i = 0; i < k; i++) {
            arr[i+left]=tmp[i];
        }



    }

    public static void mergeSortNor(int[] arr){

        int gap=1;
        while (gap<arr.length){
            for (int i = 0; i < arr.length; i+=gap*2) {
                int mid=i+gap-1;
                int left=i;
                if (mid>=arr.length){
                    mid=arr.length-1;
                }

                int right=mid+gap;
                if(right>=arr.length){
                    right=arr.length-1;
                }

                merge(arr,left,right,mid);



            }
        }


    }


}
    /**
     * @Author 12629
     * @Description：
     */
    class Sort {


        /**
         * 时间复杂度：O(N^2)
         * 最坏情况下：逆序的  5 4 3 2 1
         * 最好情况下：本身就是有序的  1 2 3 4 5 O(n)
         * 如果数据越有序，直接插入排序越快
         * 空间复杂度：O(1)
         * 稳定性：稳定的排序
         * 本身如果是一个稳定的排序，那么可以实现为不稳定的
         * 但是 如果一个排序 本身就是不稳定，能实现为稳定的排序吗？
         *
         * @param array
         */
        public static void insetSort(int[] array) {
            for (int i = 1; i < array.length; i++) {
                int tmp = array[i];
                int j = i - 1;
                for (; j >= 0; j--) {
                    if (array[j] > tmp) {
                        array[j + 1] = array[j];
                    } else {
                        array[j + 1] = tmp;


                        break;
                    }
                }
                array[j + 1] = tmp;

            }

        }

        /**
         * 不稳定的
         * 时间复杂度：n^1.3  - n^1.5
         * 空间复杂度：O(1)
         *
         * @param array
         */
        public static void shellSort(int[] array) {
            int gap = array.length;
            while (gap > 1) {
                //gap /= 2;//
                gap = gap / 3 + 1;//
                shell(array, gap);
            }
        }

        private static void shell(int[] array, int gap) {
            for (int i = gap; i < array.length; i++) {
                int tmp = array[i];
                int j = i - gap;
                for (; j >= 0; j -= gap) {
                    if (array[j] > tmp) {
                        array[j + gap] = array[j];
                    } else {
                        break;
                    }
                }
                array[j + gap] = tmp;
            }
        }

        /**
         * 选择排序：
         * 时间复杂度：O(N^2)
         * 和数据 是否有序无关
         * 空间复杂度：O(1)
         * 稳定性：不稳定的排序
         *
         * @param array
         */
        public static void selectSort2(int[] array) {

            for (int i = 0; i < array.length; i++) {
                int mindIndex = i;
                for (int j = i + 1; j < array.length; j++) {
                    if (array[j] < array[mindIndex]) {
                        mindIndex = j;
                    }
                }
                swap(array, i, mindIndex);
            }
        }

        private static void swap(int[] array, int i, int j) {
            int tmp = array[i];
            array[i] = array[j];
            array[j] = tmp;
        }


        public static void selectSort(int[] array) {
            int left = 0;
            int right = array.length - 1;
            while (left < right) {
                int minIndex = left;
                int maxIndex = left;
                for (int i = left + 1; i <= right; i++) {
                    if (array[i] < array[minIndex]) {
                        minIndex = i;
                    }
                    if (array[i] > array[maxIndex]) {
                        maxIndex = i;
                    }
                }
                swap(array, left, minIndex);
                //最大值正好是  left下标  此时 把最大值换到了minIndex的位置了
                if (maxIndex == left) {
                    maxIndex = minIndex;
                }
                swap(array, right, maxIndex);
                left++;
                right--;
            }
        }

        /**
         * 堆排序
         * 时间复杂度：O(n*logN)
         * 空间复杂度：O(1)
         * 稳定性：不稳定
         *
         * @param array
         */
        public static void heapSort(int[] array) {
            createHeap(array);
            int end = array.length - 1;
            while (end > 0) {
                swap(array, 0, end);
                siftDown(array, 0, end);
                end--;
            }
        }

        private static void createHeap(int[] array) {
            for (int parent = (array.length - 1 - 1) / 2; parent >= 0; parent--) {
                siftDown(array, parent, array.length);
            }

        }

        /**
         * @param array
         * @param parent 每棵子树调整的根节点
         * @param length 每棵子树调整的结束节点
         */
        private static void siftDown(int[] array, int parent, int length) {
            int child = 2 * parent + 1;
            while (child < length) {
                if (child + 1 < length && array[child] < array[child + 1]) {
                    child++;
                }
                if (array[child] > array[parent]) {
                    swap(array, parent, child);
                    parent = child;
                    child = 2 * parent + 1;
                } else {
                    break;
                }
            }
        }


        /**
         * 冒泡排序：
         * 时间复杂度：【讨论 没有优化的情况下，也就是 没有下方的boolean元素和-i操作】
         * O(N^2)
         * 优化以后 可能会达到O(N)
         * 空间复杂度：O(1)
         * 稳定性：稳定的排序
         *
         * @param array
         */
        public static void bubbleSort(int[] array) {
            for (int i = 0; i < array.length - 1; i++) {
                boolean flg = false;
                for (int j = 0; j < array.length - 1 - i; j++) {
                    if (array[j] > array[j + 1]) {
                        swap(array, j, j + 1);
                        flg = true;
                    }
                }
                if (!flg) {
                    break;
                }
            }
        }

        /**
         * 时间复杂度：
         * 最坏情况：当数据给定的是1 2 3 4 5 6 7.....有序的情况下 确实是O(n^2)
         * 9 8 7 6 5 4
         * 最好情况：O(N*logN)
         * 空间复杂度：
         * 最坏情况：O(N)
         * 最好情况：O(logN)
         * 稳定性：
         * 不稳定性
         *
         * @param array
         */
        public static void quickSort(int[] array) {
            //quickNor(array,0,array.length-1);
            quick(array, 0, array.length - 1);
        }

        public static void quickNor(int[] array, int start, int end) {
            Deque<Integer> stack = new ArrayDeque<>();
            int pivot = partition(array, start, end);
            if (pivot > start + 1) {
                stack.push(start);
                stack.push(pivot - 1);
            }
            if (pivot < end - 1) {
                stack.push(pivot + 1);
                stack.push(end);
            }
            while (!stack.isEmpty()) {
                end = stack.pop();
                start = stack.pop();
                pivot = partition(array, start, end);
                if (pivot > start + 1) {
                    stack.push(start);
                    stack.push(pivot - 1);
                }
                if (pivot < end - 1) {  //4:00上课
                    stack.push(pivot + 1);
                    stack.push(end);
                }
            }
        }

        private static void quick(int[] array, int start, int end) {
            if (start >= end) {
                return;
            }
            if (end - start + 1 <= 7) {
                insertSortRange(array, start, end);
                return;
            }
            //System.out.println("start: "+start+" end: "+end);
            int midIndex = getMiddleNum(array, start, end);
            swap(array, start, midIndex);

            int pivot = partition(array, start, end);
            quick(array, start, pivot - 1);
            quick(array, pivot + 1, end);
        }

        private static void insertSortRange(int[] array, int start, int end) {
            for (int i = start + 1; i <= end; i++) {
                int tmp = array[i];
                int j = i - 1;
                for (; j >= start; j--) {
                    if (array[j] > tmp) {
                        array[j + 1] = array[j];
                    } else {
                        array[j + 1] = tmp;
                        break;
                    }
                }
                array[j + 1] = tmp;
            }
        }

        private static int getMiddleNum(int[] array, int left, int right) {
            int mid = (left + right) / 2;
            if (array[left] < array[right]) {
                if (array[mid] < array[left]) {
                    return left;
                } else if (array[mid] > array[right]) {
                    return right;
                } else {
                    return mid;
                }
            } else {
                if (array[mid] > array[left]) {
                    return left;
                } else if (array[mid] < array[right]) {
                    return right;
                } else {
                    return mid;
                }
            }
        }

        private static int partition2(int[] array, int left, int right) {
            int prev = left;
            int cur = left + 1;
            while (cur <= right) {
                if (array[cur] < array[left] && array[++prev] != array[cur]) {
                    swap(array, cur, prev);
                }
                cur++;
            }
            swap(array, prev, left);
            return prev;
        }

        private static int partition(int[] array, int left, int right) {
            int tmp = array[left];
            while (left < right) {
                while (left < right && array[right] >= tmp) {
                    right--;
                }
                array[left] = array[right];
                while (left < right && array[left] <= tmp) {
                    left++;
                }
                array[right] = array[left];
            }
            array[left] = tmp;
            return left;
        }

        private static int partitionHoare(int[] array, int left, int right) {
            int tmp = array[left];
            int tmpLeft = left;
            while (left < right) {
                while (left < right && array[right] >= tmp) {
                    right--;
                }
                while (left < right && array[left] <= tmp) {
                    left++;
                }
                swap(array, left, right);
            }
            swap(array, left, tmpLeft);
            return left;
        }

        /**
         * 归并排序：
         * 时间复杂度：O(N*logN)
         * 空间复杂度：O(N)
         * 稳定性：稳定排序
         *
         * @param array
         */
        public static void mergeSort(int[] array) {

            mergeSortTmp(array, 0, array.length - 1);
        }

        private static void mergeSortTmp(int[] array, int left, int right) {
            if (left >= right) {
                return;
            }
            int mid = (left + right) / 2;
            mergeSortTmp(array, left, mid);
            mergeSortTmp(array, mid + 1, right);
            //走到这里 全部分解完毕
            // 合并
            merge(array, left, mid, right);
        }

        private static void merge(int[] array, int left, int mid, int right) {
            int[] tmp = new int[right - left + 1];
            int k = 0;
            int s1 = left;
            //int e1 = mid;
            int s2 = mid + 1;
            //int e2 = right;
            while (s1 <= mid && s2 <= right) {
                if (array[s1] <= array[s2]) {
                    tmp[k++] = array[s1++];
                } else {
                    tmp[k++] = array[s2++];
                }
            }
            while (s1 <= mid) {
                tmp[k++] = array[s1++];
            }
            while (s2 <= right) {
                tmp[k++] = array[s2++];
            }
            //可以保证tmp数组 是有序的
            for (int i = 0; i < k; i++) {
                array[i + left] = tmp[i];
            }
        }

        /**
         * 非递归实现 归并排序
         *
         * @param array
         */
        public static void mergeSortNor(int[] array) {
            int gap = 1;
            while (gap < array.length) {
                for (int i = 0; i < array.length; i = i + gap * 2) {
                    int left = i;
                    int mid = left + gap - 1;
                    if (mid >= array.length) {
                        mid = array.length - 1;
                    }
                    int right = mid + gap;
                    if (right >= array.length) {
                        right = array.length - 1;
                    }
                    merge(array, left, mid, right);
                }
                gap *= 2;
            }
        }

        /**
         * 计数排序：
         * 时间复杂度：O(范围 + n )
         * 范围越大  越慢
         * 空间复杂度：O(范围)
         * 稳定性：
         *
         * @param array
         */
        public static void countSort(int[] array) {
            //1. 找最大值 和 最小值 来确定 计数数组的大小
            int maxVal = array[0];
            int minVal = array[0];
            for (int i = 1; i < array.length; i++) {
                if (array[i] < minVal) {
                    minVal = array[i];
                }
                if (array[i] > maxVal) {
                    maxVal = array[i];
                }
            }
            int len = maxVal - minVal + 1;
            int[] count = new int[len];

            //2. 遍历原来的数组array把 每个元素 放到对应的计数数组当中 进行计数
            for (int i = 0; i < array.length; i++) {
                int index = array[i];
                count[index - minVal]++;
            }
            //3.依次 遍历计数数组 O(范围)
            int index = 0;
            for (int i = 0; i < count.length; i++) {
                while (count[i] != 0) {
                    array[index] = i + minVal;
                    index++;
                    count[i]--;
                }
            }
        }
    }






